package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.xml.crypto.Data;

import main.DataTransferObject;
import main.MyConnection;

public class UserDAOImpl implements UserDAO {

	@Override
	public boolean createOrUpdate(DataTransferObject inDTO) {
		try{
			Connection conn = MyConnection.getConnection();
			String sql = "SELECT * FROM User WHERE Username = ?";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, inDTO.getUserName());
			ps.execute();
			ResultSet rs = ps.getResultSet();
			
			//If username exists, do update
			if(rs.next()){ 
				//Do nothing, if more attributes are added to the User Table,
				//then this will have an update sql...
			} else {
				sql = "INSERT INTO User (Username) values (?)";
				ps = conn.prepareStatement(sql);
				ps.setString(1, inDTO.getUserName());
				ps.execute();
			}
		} catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	@Override
	public int getUserID(String username){
		try{
			Connection conn = MyConnection.getConnection();
			String sql = "SELECT * FROM User WHERE Username = ?";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, username);
			ps.execute();
			ResultSet rs = ps.getResultSet();
			
			if(rs.next()){ 
				return rs.getInt("User_ID");
			} 
		} catch(Exception e){
			e.printStackTrace();
			return -1;
		}
		return -1;
	}

	@Override
	public int getNumberOfUsers() {
		int total = 0;
		try{
			Connection conn = MyConnection.getConnection();
			String sql = "SELECT * FROM User";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.execute();
			ResultSet rs = ps.getResultSet();
			
			while(rs.next()){
				total++;
			}
		} catch(Exception e){
			e.printStackTrace();
			return -1;
		}
		return total;
	}

	@Deprecated
	@Override
	public List<DataTransferObject> getUsers(DataTransferObject criteria) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<DataTransferObject> getAllUsers() {
		ArrayList<DataTransferObject> dtoList = new ArrayList<DataTransferObject>();
		try{
			Connection conn = MyConnection.getConnection();
			String sql = "SELECT * FROM User";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.execute();
			ResultSet rs = ps.getResultSet();
			
			while(rs.next()){
				DataTransferObject dto = new DataTransferObject();
				dto.setUserID(rs.getInt("User_ID"));
				dto.setUserName(rs.getString("Username"));
				dtoList.add(dto);
			}
		} catch(Exception e){
			e.printStackTrace();
			return null;
		}
		return dtoList;
	}
	
	@Override
	public boolean doesUserExist(String username) {
		ArrayList<DataTransferObject> dtoList = new ArrayList<DataTransferObject>();
		try{
			Connection conn = MyConnection.getConnection();
			String sql = "SELECT * FROM User WHERE Username = ?";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, username);
			ps.execute();
			ResultSet rs = ps.getResultSet();
			
			if(rs.next()){
				return true;
			}
		} catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return false;
	}

}
